package com.yonyou.pmclouds.workstopreport;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.outter.user.UserOperator;
import com.yonyou.pmclouds.project.controller.ProjectCommonRefController;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.team.rmiitf.ProjectRefWithTeamQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workstartreport.entity.WorkStartVO;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopMaintain;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping({"/mobile/workStop","/workStop"})
@RestController
public class WorkStopController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkStopMaintain workStopMaintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkStopQuery workStopQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectRefWithTeamQuery query;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;
    @Autowired
    private UserOperator userOperator;
    @Autowired
    private ProjectCommonRefController pcr;
    @Autowired
    private UserInfoQuery userInfoQuery;
    private static final String[] USER_ID_FIELDS = {WorkStartVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {WorkStartVO.CREATOR_NAME};



    //添加
    @Insert
    @PMApprove
    @RequestMapping("/add")
    public WorkStopVO add(@RequestBody WorkStopVO workStopVO) throws BusinessException {
        //补全字段
        initInsertVO(workStopVO);
        //智能日志需要制单人信息
        String creatorName = userInfoQuery.queryUserName(RuntimeEnvironment.getLoginUserId());
        workStopVO.setCreatorName(creatorName);
        //调用Service层的添加方法
        WorkStopVO insert = workStopMaintain.insert(workStopVO);
        return insert;
    }


    //修改
    @PMApprove(trigger = "commit")
    @RequestMapping("/update")
    public WorkStopVO update(@RequestBody WorkStopVO workStopVO) throws BusinessException {
        //直接调用Service层的update方法
        WorkStopVO update = workStopMaintain.update(workStopVO);
        //为返回的对象追加创建者姓名这个属性，在这可加可不加，看需求
        userQuery.tranUserInfoField(new WorkStopVO[]{update}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return update;
    }

    //查询停工报告总数量
    @RequestMapping("/counts")
    //无用
    public Object counts(){
        Integer counts = workStopQuery.counts();
        return counts;
    }

    //通过报告ID查询停工报告
    @RequestMapping("/getReportById")
    public Object getReportByProjectId(String reportId) throws BusinessException {
        //调用Service的方法
        WorkStopVO workStopVO = workStopQuery.getReport(reportId);

        List<CopyPersonVO> copyPersonVOList = workStopVO.getCopyPersonVOList();
        //翻译抄送人名称
        userQuery.tranUserInfoField(copyPersonVOList.toArray(new CopyPersonVO[0]), USER_ID_FIELDS, USER_NAME_FIELDS);

        //追加创建者姓名
        userQuery.tranUserInfoField(new WorkStopVO[]{workStopVO}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return workStopVO;
    }

    //通过项目ID查询项目信息
    @RequestMapping("/getProjectById")
    public Object getProjectById(String projectId){
        ProjectVO projectById = workStopQuery.getProjectById(projectId);
        return projectById;
    }

    //删除
    @RequestMapping("/remove")
    public void remove(@RequestBody WorkStopVO workStopVO) throws BusinessException {
        workStopMaintain.delete(workStopVO);
    }

    private void initInsertVO(WorkStopVO vo){
        if(vo == null) return;
        //单据类型 类型‘TGBG’
        vo.setBillType(BillTypeConst.WORKSTOP_BILLTYPE);
        //单据状态 添加为自由态5
        vo.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
    }

}
